package org.jsbd.boss.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.dao.IEmailGroupDao;
import org.jsbd.boss.domian.EmailGroupBean;
import org.jsbd.boss.domian.EmailGroupRef;
import org.jsbd.boss.service.IEmailGroupService;

import com.alibaba.dubbo.common.utils.StringUtils;

@Service("emailGroupService")
public class EmailGroupServiceImpl implements IEmailGroupService {

	@Autowired
	private IEmailGroupDao emailGroupDao;

	@Override
	public void saveEmailGroup(EmailGroupBean emailGroup){
		emailGroupDao.saveEmailGroup(emailGroup);
	}

	@Override
	public void updateEmailGroup(EmailGroupBean emailGroup){
		emailGroupDao.updateEmailGroup(emailGroup);
	}

	@Override
	public EmailGroupBean getEmailGroup(EmailGroupBean emailGroup) {
		return emailGroupDao.getEmailGroup(emailGroup);
	}

	@Override
	public List<EmailGroupBean> getEmailGroupList(EmailGroupBean emailGroup) {
		return emailGroupDao.getEmailGroupList(emailGroup);
	}

	@Override
	public Map<String, Object> getEmailGroupListPage(DataGridModel page, EmailGroupBean emailGroup) {
		return emailGroupDao.getEmailGroupListPage(page, emailGroup);
	}

	@Override
	public Integer getEmailGroupCount(EmailGroupBean emailGroup) {
		return emailGroupDao.getEmailGroupCount(emailGroup);
	}
	
	@Override
	public void deleteEmailGroup(EmailGroupBean emailGroup) {
		emailGroupDao.deleteEmailGroup(emailGroup);
	}

	@Override
	public Map<String, Object> getOutEmailListPage(DataGridModel page, EmailGroupRef form) {
		return emailGroupDao.getOutEmailListPage(page, form);
	}

	@Override
	public Map<String, Object> getInEmailListPage(DataGridModel page, EmailGroupRef form) {
		return emailGroupDao.getInEmailListPage(page, form);
	}

	@Override
	public void saveEmailGroupRef(EmailGroupRef emailGroupRef) {
		if(StringUtils.isBlank(emailGroupRef.getEmailIdStr())){
			emailGroupDao.saveEmailGroupRef(emailGroupRef);
		}else{
			String[] emalIdArr=emailGroupRef.getEmailIdStr().split(",");
			List<EmailGroupRef> emailGroupRefList=new ArrayList<EmailGroupRef>(emalIdArr.length);
			for(int i=0;i<emalIdArr.length;i++){
				EmailGroupRef temp=new EmailGroupRef();
				temp.setEmailId(Integer.valueOf(emalIdArr[i]));
				temp.setEmailGroupId(emailGroupRef.getEmailGroupId());
				emailGroupRefList.add(temp);
			}
			emailGroupDao.saveEmailGroupRef(emailGroupRefList);
		}
	}

	@Override
	public void deleteEmailGroupRef(EmailGroupRef emailGroupRef) {
		if(StringUtils.isBlank(emailGroupRef.getEmailIdStr())){
			emailGroupDao.deleteEmailGroupRef(emailGroupRef);
		}else{
			String[] emalIdArr=emailGroupRef.getEmailIdStr().split(",");
			List<EmailGroupRef> emailGroupRefList=new ArrayList<EmailGroupRef>(emalIdArr.length);
			for(int i=0;i<emalIdArr.length;i++){
				EmailGroupRef temp=new EmailGroupRef();
				temp.setEmailId(Integer.valueOf(emalIdArr[i]));
				temp.setEmailGroupId(emailGroupRef.getEmailGroupId());
				emailGroupRefList.add(temp);
			}
			emailGroupDao.deleteEmailGroupRef(emailGroupRefList);
		}
		
	}
	
}
